{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='../../plugin/lottery/template/web/lottery_common_nav'}
    <div class="panel panel-default" role="tabpanel">
        <div class="panel-heading">基本信息</div>
        <div class="panel-body">
            <form action="" method="post" class="form-horizontal" role="form" name="form2" id="form2">
                <input type="hidden" name="p_id" value="{$item['p_id']}"/>
                <input type="hidden" name="themeid" value="{$themeid}"/>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品属性</label>
                    <div class="col-sm-8 col-xs-12">
                        <label class="radio-inline">
                            <input name="default_type" value="1" {if $item['default_type']==1 || !$item['default_type']} checked="checked"{/if} type="radio"> 正常奖项
                        </label>
                        <label class="radio-inline">
                            <input name="default_type" value="3" {if $item['default_type']==3} checked="checked"{/if} type="radio"> 必中奖项
                        </label>
                        <label class="radio-inline">
                            <input name="default_type" value="2" {if $item['default_type']==2} checked="checked"{/if} type="radio"> 谢谢参与
                        </label>
                        <!--                        <span class="help-block">如：</span>-->
                    </div>
                </div>
                <input type="hidden" name="day_qty" value="{$item['day_qty']}">
                <input type="hidden" name="pr" value="{$item['pr']}">
                <input type="hidden" name="show_pr" value="{$item['show_pr']}">
<!--                <div class="form-group">-->
<!--                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label">奖项名称</label>-->
<!--                    <div class="col-sm-8 col-xs-12">-->
<!--                        <input type="text" class="form-control"  placeholder="" name="name"  value="{$item['name']}">-->
<!--                        <span class="help-block">示例：一等奖,非必填；不填写默认为奖品名称</span>-->
<!--                    </div>-->
<!--                </div>-->
                <div class="form-group">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品名称</label>
                    <div class="col-sm-8 col-xs-12 lotteryConfig" >
                        <input type="text" class="form-control"  placeholder="" name="title"  value="{$item['title']}">
                        <span class="help-block"> 必填项；限8个汉字以内；<span style="color: red;">此处更改会影响所有已中奖记录；</span>如需更换奖品，请下架此奖品；再新增奖品</span>
                    </div>
                    <div class="col-sm-8 col-xs-12 notLotteryConfig">
                        <select  class="form-control" id="notLottery">
                            {loop $not_lottery $key  $row}
                                   <option value="{$row['title']}" data-img="{$row['small_img']}"   {if $row['title'] == $item['title']} selected="selected" {/if}>{$row['title']} </option>
                            {/loop}
                        </select>
                    </div>
                </div>

                <div class="form-group lotteryConfig">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;"></span>奖品介绍</label>
                    <div class="col-sm-8 col-xs-12">
                        <textarea type="text" rows="3" class="form-control desc" placeholder="" name="desc" >{$item['desc_content']}</textarea>
                        <span class="help-block">控制在100个字以内</span>
                    </div>
                </div>

                <div class="form-group lotteryConfig">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>可用库存</label>
                    <div class="col-sm-8 col-xs-12">
                        <input type="number" class="form-control"  placeholder="" name="qty"  value="{$item['qty']}">
                    </div>
                </div>

                {if !$config['yx_lt_double_flag']}
                <div class="form-group lotteryConfig" id="day_qty">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>每日发放上限</label>
                    <div class="col-sm-8 col-xs-12">
                        <input type="number" class="form-control"   name="day_qty"  value="{$item['day_qty']}">
                    </div>
                </div>
                {/if}
                {if $system['fenxiao']  && $_C['yx_lt_open_red_package']==1}
                <div class="form-group lotteryConfig">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品类型</label>
                    <div class="col-sm-8 col-xs-12">
                        <label class="radio-inline">
                            <input name="type" value="default" {if $item['type']=='default' || !$item['type']} checked="checked"{/if} type="radio"> 实物奖品
                        </label>
                        {if $_C['yx_lt_open_red_package']==1}
                        <label class="radio-inline">
                            <input name="type" value="red_envelope" {if $item['type']=='red_envelope'} checked="checked"{/if} type="radio"> 红包奖品
                        </label>
                        {/if}
                    </div>
                </div>
                {/if}

                <div class="form-group redEnvelopeConfig" id="red_envelopes_day_qty" >
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label">每日红包发放数量</label>
                    <div class="col-sm-8 col-xs-12">
                        <input type="number" class="form-control"  placeholder="" name="red_envelopes_day_qty"  value="{$item['red_envelopes_day_qty']}">
                    </div>
                </div>

                <div class="form-group redEnvelopeConfig" id="red_envelopes_amount" >
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label">单个红包金额</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group">
                            <input type="number" class="form-control"  placeholder="发放红包金额最小值，最小值为0.3元" name="red_envelopes_amount_min"  value="{$item['red_envelopes_amount_min']}">
                            <span class="input-group-addon">-</span>
                            <input type="number" class="form-control"  placeholder="发放金额最大值" name="red_envelopes_amount_max"  value="{$item['red_envelopes_amount_max']}">
                        </div>
                        <span class="help-block">如：最小金额为0.3元,发放金额为最小值与最大值区间随机金额，如需固定金额将最小值和最大值设置为相同值即可</span>
                    </div>
                </div>
                <div class="form-group" hidden="hidden">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品展示排序</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="col-sm-4 col-xs-12 input-group">
                            <input class="form-control" type="number"  placeholder="1" maxlength="2" name="show_sort" id="show_sort"  value="{php echo $item['show_sort'];}">
                            <span class="input-group-addon">位</span>
                        </div>
                        <span class="help-block">排名靠前 数值越小 最小为1</span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品状态</label>
                    <div class="col-sm-8 col-xs-12">
                        <label class="radio-inline">
                            <input name="status" value="1" {if $item['status']==1} checked="checked"{/if} type="radio"> 上架
                        </label>
                        <label class="radio-inline">
                            <input name="status" value="2" {if $item['status']==2} checked="checked"{/if} type="radio"> 下架
                        </label>
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>奖品小图</label>

                    <div class="col-sm-8 col-xs-12 lotteryConfig">
                        {php echo view_form_field_image('small_img',$item['small_img']);}
                        <span class="help-block">图片尺寸：200*200像素</span>
                    </div>
                     <div class="col-sm-8 col-xs-12 notLotteryConfig">
                             <img src="" alt="" id="show_small_img" >
<!--                             <input type="hidden" name="small_img" id="small_img" value="">-->
                     </div>
                </div>


                <div class="form-group" id="sub_upload" >
                    <div class="col-sm-12">
                        <input name="submit" onclick="addOrEditItem()" id="btnConfirm" type="button" value="提交" class="btn btn-primary col-lg-2">
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

<div style="position: fixed;
    top: 600px;
    right: 100px;
    width: 100px;">
    <div class="col-sm-12">
        <input name="submit" onclick="addOrEditItem()" id="btnConfirm1" type="button" value="提交" class="btn btn-primary col-lg-2" style="width: 100px;">
    </div>
</div>


<script type="text/javascript">
    $(document).ready(function () {
        var defaultType = $("input[name='default_type']:checked").val();
        changeDefaultType(defaultType);
        var type = $("input[name='type']:checked").val();
        changeType(type);
    });

    $("input[name='type']").click(function() {
        var value = $(this).val();
        changeType(value)
    });

   $('#notLottery').change(function () {
       var img = $(this).find(':selected').data('img')
       $('#show_small_img').attr('src',img);
       $("input[name='small_img']").val(img);
       $("input[name='title']").val($(this).val());
   })

    //类型切换
    function changeType(value) {
        if (value=='red_envelope') {
            $(".redEnvelopeConfig").show();
            $("#total_qty").hide();
            $("#out_qty").hide();
            $("#day_qty").hide();
        } else {
            $(".redEnvelopeConfig").hide();
            $("#total_qty").show();
            $("#out_qty").show();
            $("#day_qty").show();
        }
    }

    //切换属性
    $('input:radio[name="default_type"]').click(function(){
         var value = $(this).val();
         changeDefaultType(value);
    });

    function changeDefaultType(value){
        if(value==2){    //谢谢参与
            $('.lotteryConfig').hide();
            $(".redEnvelopeConfig").hide();
            $('.notLotteryConfig').show();

            var img =$('#notLottery').find(':selected').data('img')
            $('#show_small_img').attr('src',img);
            $("input[name='small_img']").val(img);
            var title = $('#notLottery').val();
            $("input[name='title']").val(title)
        }else{
            $('.lotteryConfig').show();
            $('.notLotteryConfig').hide();
            var type = $("input[name='type']:checked").val();
            changeType(type);
        }
    }

    //新增\修改奖品 提交数据
    function addOrEditItem() {
        // if ($("#show_sort").val().length>2){
        //     layer.msg('奖品展示排名，请设置两位数以内的排序值');
        //     return;
        // }
        // if ($(".desc").val().length>100){
        //     layer.msg('奖品介绍请设置100个字以内');
        //     return;
        // }
        let postUrl = "{php echo createWebUrlPlugin('lottery.add',array('themeid'=>$themeid));}";
        let success_redirect = "{php echo createWebUrlPlugin('lottery',array('themeid'=>$themeid));}";
        let formData = $("#form2").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
    window.addEventListener("keydown", function(e) {
        //可以判断是不是mac，如果是mac,ctrl变为花键
        //event.preventDefault() 方法阻止元素发生默认的行为。
        if((e.key=='s'||e.key=='S')&&(navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
            e.preventDefault();
            addOrEditItem();
        }
    }, false);
</script>
